<?php
$code = <<<EOF
package service

import (
    "{$data['root_path_name']}/{$data["service_name"]}/model"
	"context"
	"errors"
	"fmt"
	"git.inke.cn/BackendPlatform/golang/logging"
	"math"
)

/**
 * @Description: 新增
 * @param ctx
 * @param req
 * @return err
 */
func (s *Service) {$data['model_name']}Add(ctx context.Context, req model.{$data['model_name']}AddReq) (record *model.{$data['model_name']}Model, err error) {
	logger := logging.For(ctx, "func", "{$data['model_name']}Add", "req", req)
	//入库
	record, err = s.dao.Add{$data['model_name']}(ctx, req)
	if err != nil {
		logger.Errorf("{$data['model_name']}Add err: %v", err)
		return
	}

	if record == nil {
		logger.Errorf("{$data['model_name']}Add err，record is nil")
	}

	// todo 1、add可能需要处理额外逻辑
	return
}


/**
 * @Description: 【软删除】
 * @param ctx
 * @param req
 * @return err
 */
func (s *Service) {$data['model_name']}Delete(ctx context.Context, id int64) (err error) {
	logger := logging.For(ctx, "func", "{$data['model_name']}Delete", "id", id)
	//先查询这条数据是否存在
	record, err := s.dao.Get{$data['model_name']}ById(ctx, id)
	if record == nil {
		return code.DbNoDataError
	}

	//软删除
	where := map[string]interface{}{"id": id}
	updateData := map[string]interface{}{"status": model.{$data['model_name']}StatusDeleted}//todo 设置status常量
	err = s.dao.Update{$data['model_name']}ByCondition(ctx, where, updateData)
	if err != nil {
		logger.Errorf("{$data['model_name']}Add err: %v", err)
		return err
	}

	// todo 2、delete可能需要处理额外逻辑
	return
}

/**
 * @Description: 更新
 * @param ctx
 * @param req：里面必须包含唯一键参数，默认为id
 * @return error
 */
func (s *Service) {$data['model_name']}Update(ctx context.Context, req map[string]interface{}) error {
	logger := logging.For(ctx, "func", "{$data['model_name']}Update", "req", req)
	fId, ok := req["id"].(float64)
	if !ok {
		return errors.New("uid must be int64")
	}

	id := int64(fId)//todo 默认为id，请自行修改
	where := map[string]interface{}{"id":id}
	delete(req, "id")//主键放where条件里，不需要更新了
	err := s.dao.Update{$data['model_name']}ByCondition(ctx, where, req)
	if err != nil {
		logger.Errorf("{$data['model_name']}Update err, id: %v, profile: %v,err:%v", id, req, err)
		return err
	}
	
	return nil
}

/** 
 * @Description: 详情
 * @param ctx
 * @param id
 * @return detail
 * @return err
 */
func (s *Service) {$data['model_name']}Detail(ctx context.Context, req model.{$data['model_name']}DetailReq) (resp model.{$data['model_name']}DetailResp, err error) {
	logger := logging.For(ctx, "func", "{$data['model_name']}Detail", "req", req)

	key := s.dao.Get{$data['model_name']}ItemCacheKey(req.Id)
	has, detailInfo, err := s.dao.Get{$data['model_name']}Cache(ctx, key)
	if !has {
		dbDetailInfo, err := s.dao.Get{$data['model_name']}ById(ctx, req.Id)
		if err != nil {
			logger.Errorf("err: %v", err)
			return resp, code.DbNoDataError
		}
		_ = s.dao.Add{$data['model_name']}Cache(ctx, *dbDetailInfo, key, model.{$data['model_name']}ItemExpireTime)
	}

	resp.Data = detailInfo
	return
}

/** 
 * @Description: 查询
 * @param ctx
 * @param req
 * @return model.{$data['model_name']}ListResp
 */
func (s *Service) {$data['model_name']}List(ctx context.Context, req model.{$data['model_name']}ListReq) model.{$data['model_name']}ListResp {//todo 定义struct
	offset := (req.Page-1) * req.Size
	totalPage := 0
	where := map[string]interface{}{
//		"status":model.{$data['model_name']}StatusNormal,//todo 定义查询条件，必填
	}

	list,totalNum := s.dao.Find{$data['model_name']}ByPage(ctx, where,offset,req.Size,"id desc",fmt.Sprintf("status != %v",model.{$data['model_name']}StatusDeleted))

	resList := make([]model.{$data['model_name']}Item,0)
	if len(list) > 0 {
		for _,v := range list{
			tmp := model.{$data['model_name']}Item{
				Id:           v.Id,
				//todo list是Model的数据，需要转换成接口需要的数据
			}
			resList = append(resList,tmp)
		}
	}

	if totalNum > 0{
		totalPage = int(math.Ceil(float64(totalNum) / float64(req.Size)))
	}

	res := model.{$data['model_name']}ListResp{
		TotalNum:  totalNum,
		TotalPage: totalPage,
		Page:      req.Page,
		List:      resList,
	}
	return res
}
EOF;
?>
<?= $code?>
